﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
' <copyright file="ThumbnailImage.vb" company="Microsoft">
'     Copyright © Microsoft Corporation. All rights reserved.
' </copyright>
' <summary>Implements the ThumbnailImage class</summary>
' <author>Microsoft Expression Encoder Team</author>

Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media.Imaging

Namespace ExpressionMediaPlayer
	''' <summary>
	'''  Similar function as the Image control -- but handles UriFormatException cases by leaving image blank instead of thowing the exception higher.
	''' </summary>
	Public Class ThumbnailImage
		Inherits ContentControl
		''' <summary>
		''' Using a DependencyProperty as the backing store for SourceUrl.  This enables animation, styling, binding, etc...
		''' </summary>
		Public Shared ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(ThumbnailImage), New PropertyMetadata(New PropertyChangedCallback(AddressOf SourcePropChanged)))
		''' <summary>
		''' Initializes a new instance of the ThumbnailImage class.
		''' </summary>
		Public Sub New()
		End Sub
		''' <summary>
		''' Gets or sets the source of this thumbnail.
		''' </summary>
		Public Property Source() As String
			Get
				Return CType(GetValue(SourceProperty), String)
			End Get
			Set(ByVal value As String)
				SetValue(SourceProperty, value)
			End Set
		End Property
		''' <summary>
		''' Load image
		''' </summary>
		Public Sub LoadImage()
			Try
				Dim sourceEscaped As String = IsoUri.EscapeUriString(Me.Source)
				Dim thumbnailUri As New IsoUri(sourceEscaped)
				Dim image As New Image()
				If thumbnailUri.IsoFileExists Then
					Dim bi As New BitmapImage()
					bi.SetSource(thumbnailUri.Stream)
					image.Source = bi
				Else
					Dim bi As New BitmapImage(thumbnailUri.FullyQualifiedUri)
					image.Source = bi
				End If
				AddHandler image.ImageFailed, AddressOf OnImageFailed
				Me.Content = image
			Catch e1 As UriFormatException
				' leave thumbnail blank if image can't be loaded                
			End Try
		End Sub
		''' <summary>
		''' Property handler for the source dependency property.
		''' </summary>
		''' <param name="imageObject">Source dependency object.</param>
		''' <param name="eventArgs">Event args.</param>
		Protected Shared Sub SourcePropChanged(ByVal imageObject As DependencyObject, ByVal eventArgs As DependencyPropertyChangedEventArgs)
			Dim thumbnailImage As ThumbnailImage = TryCast(imageObject, ThumbnailImage)
			If thumbnailImage IsNot Nothing AndAlso eventArgs.NewValue IsNot Nothing AndAlso TypeOf eventArgs.NewValue Is String Then
				ThumbnailDownloader.Enqueue(thumbnailImage)
			End If
		End Sub
		''' <summary>
		''' Event handler for the Image failed event from the image.
		''' </summary>
		''' <param name="sender">Source of the event.</param>
		''' <param name="e">Event args.</param>
		Private Sub OnImageFailed(ByVal sender As Object, ByVal e As ExceptionRoutedEventArgs)
			Dim strErrorMessage As String = String.Empty
			If Me.Source IsNot Nothing Then
				strErrorMessage &= Me.Source.ToString() & vbCrLf
			End If
			strErrorMessage &= e.ErrorException.ToString() & vbCrLf

			Dim textBlock As New TextBlock()
			textBlock.Text = strErrorMessage
			textBlock.TextWrapping = TextWrapping.Wrap

			Content = textBlock
		End Sub
	End Class
End Namespace
